<template>
  <div>
    <el-container>
      <el-header>
        <div class="left_box">
          <img src="../assets/heima.png" alt="" />
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="doLogout">退出</el-button>
      </el-header>
      <el-container>
        <!-- 侧边栏逻辑：添加一个点击事件troggleCollapse，然后给菜单动态绑定一个折叠展开属性collapse='isCollapse'，
        然后data中设置默认为isCollapse='false'。菜单栏有两种值，折叠或展开，于是给点击按钮isCollapse设置取反。
        给侧边栏宽度设置绑定属性iscollapse为true，宽度为64；反之，宽度为200-->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 添加一个点击事件，点击之后就会折叠或展开 -->
          <div class="triggle_btn" @click="troggleCollapse">|||</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            unique-opened
            :default-active="activePath"
          >
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <!-- 记着图标class加冒号 -->
                <i :class="iconsObj[item.id]"></i>
                <span slot="title">{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'/' + subItem.path"
                v-for="subItem in item.children"
                :key="subItem.id"
                @click="saveNavState('/' + subItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{ subItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- 路由占位符 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
  <!-- 路由高光效果：首先点击路由或者刷新菜单都变蓝
       逻辑：给子路由添加点击事件，传递被激活的index参数，将index保存在sessionstorage中，data中声明。
       将保存的index定义到menu的default-active属性中。加载的时候就有的高光，在create中将activepath从sessionstorage中取出
       最后methods中更新activepath -->
</template>


<script>
export default {
  data() {
    return {
      menuList: [],
      iconsObj: {
        125: "iconfont icon-user",
        103: "iconfont icon-tijikongjian",
        101: "iconfont icon-shangpin",
        102: "iconfont icon-danju",
        145: "iconfont icon-baobiao",
      },
      isCollapse: false,
      activePath: "",
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem('activePath');
  },

  methods: {
    doLogout() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 请求加载菜单栏之前要开启请求拦截，携带token发送请求
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      // console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.menuList = res.data;
    },
    troggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    },
  },
};
</script>

<style lang="scss" scoped>
.el-container {
  height: 100vh;
  .el-header {
    background-color: #373d41;
    padding-left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left_box {
      display: flex;
      align-items: center;
      > span {
        color: #fff;
        font-size: 20px;
        margin-left: 15px;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .triggle_btn {
      background-color: #4a5064;
      color: #fff;
      text-align: center;
      line-height: 24px;
      cursor: pointer;
      letter-spacing: 0.2em;
      font-size: 10px;
    }
    .el-menu {
      border-right: 0;
    }
    .iconfont {
      margin-right: 10px;
    }
  }
  .el-main {
    background-color: #eaedf1;
  }
}
</style>